package MyFramePro.Service.Impl;

import MyFramePro.Constants.SystemConstants;
import MyFramePro.Domain.Entity.Article;
import MyFramePro.Domain.Entity.Category;
import MyFramePro.Domain.ResponseResult;
import MyFramePro.Domain.Vo.CategoryVo;
import MyFramePro.Domain.Vo.PageVo;
import MyFramePro.Mapper.CategoryMapper;
import MyFramePro.Service.CategoryService;
import MyFramePro.Utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-04-30 15:21:35
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private ArticleServiceImpl articleService;  //文章表

    @Resource
    private CategoryServiceImpl categoryService;   //分类表

    /**
     * 这里是进行获取分类的集合
     */
    @Override
    public ResponseResult getCategoryList() {
        //1.先查询状态为已发布的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(queryWrapper);

        //2.获取文章的 Category 分类ID，并且去重
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());   //这里进行 Set 去重操作

        //3.根据分类 ID 在分类表中遍历查询对应的分类的名字
        List<Category> categoryList = listByIds(categoryIds);   //根据去重后的 ID 查询到的分类信息集合

        List<Category> categoriesRes = categoryList.stream()
                .filter(category -> SystemConstants.CATEGORY_STATUS_NORMAL.equals(category.getStatus()))  //这里进行条件的过滤 (状态为正常的)
                .collect(Collectors.toList());

        //4.封装成 VO 类型
        List<CategoryVo> categoryVos = BeanCopyUtils.myCopyBeanList(categoriesRes, CategoryVo.class);

        return ResponseResult.okResult(categoryVos);
    }


    /**
     * 这里是查询分类集合的功能
     */
    @Override
    public List<CategoryVo> listAllCategory() {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //1.查询当前状态为 0 ，即正常状态的分类信息
        queryWrapper.eq(Category::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);

        List<Category> categories = categoryService.list(queryWrapper);

        //2.由于前端需求的字段，这里使用 Vo 类型进行封装
        List<CategoryVo> categoryVoList = BeanCopyUtils.myCopyBeanList(categories, CategoryVo.class);

        return categoryVoList;
    }


    /**
     * 这里是分页查询分类列表的功能
     */
    @Override
    public ResponseResult getCagegoryPageList(Integer pageNum, Integer pageSize, String name, String status) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //1.进行条件过滤
        //1.1能根据分类名称进行模糊查询
        queryWrapper.like(StringUtils.hasText(name),Category::getName,name);
        //1.2能根据状态进行查询
        if(StringUtils.hasText(status)) {
            queryWrapper.eq(Category::getStatus, status);
        }else {
            queryWrapper.eq(Category::getStatus,SystemConstants.CATEGORY_STATUS_NORMAL);    //若没有传入状态，默认查询正常状态的分类信息
        }

        List<Category> categoryList = categoryService.list(queryWrapper);

        //2.分页查询
        Page<Category> page = new Page<>(pageNum,pageSize);
        categoryService.page(page, queryWrapper);

        //3.由于前端的需求字段，这里需要转换为 Vo 类型进行封装
        PageVo pageVo = new PageVo(categoryList,page.getTotal());

        return ResponseResult.okResult(pageVo);
    }


    /**
     * 这里是新增分类的功能
     */
    @Override
    public ResponseResult addCategory(CategoryVo categoryVo) {

        Category category = BeanCopyUtils.myCopyBean(categoryVo, Category.class);
        categoryService.save(category);

        return ResponseResult.okResult();
    }


    /**
     * 修改分类————查询分类信息功能
     */
    @Override
    public ResponseResult selectCatrgoryInfo(Long id) {

        Category category = categoryService.getById(id);
        CategoryVo categoryVo = BeanCopyUtils.myCopyBean(category, CategoryVo.class);

        return ResponseResult.okResult(categoryVo);
    }


    /**
     * 修改分类————更新分类功能
     */
    @Override
    public ResponseResult updateCategory(CategoryVo categoryVo) {

        Category category = BeanCopyUtils.myCopyBean(categoryVo, Category.class);
        categoryService.updateById(category);

        return ResponseResult.okResult();
    }


    /**
     * 这里是删除分类的功能
     */
    @Override
    public ResponseResult deleteCategory(Long id) {

        categoryService.removeById(id);

        return ResponseResult.okResult();
    }


}

